//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartArea3DView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartArea3DView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartArea3DView

IMPLEMENT_DYNCREATE(CChartArea3DView, CBCGPChartExampleView)

CChartArea3DView::CChartArea3DView()
	: CBCGPChartExampleView(CChartArea3DView::IDD)
{
	//{{AFX_DATA_INIT(CChartArea3DView)
	m_nTransparency = 0;
	m_bShowDataLabels = FALSE;
	m_nStackType = 0;
	m_nDepth = 200;
	m_nGapDepth = 50;
	m_nHeight = 100;
	m_bShowWalls = TRUE;
	m_bThickWalls = TRUE;
	//}}AFX_DATA_INIT
}

CChartArea3DView::~CChartArea3DView()
{
}

void CChartArea3DView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartArea3DView)
	DDX_Control(pDX, IDC_THICK_WALLS, m_wndThickWalls);
	DDX_Control(pDX, IDC_STACK_TYPE, m_wndStackType);
	DDX_Control(pDX, IDC_STACK_TYPE_LABEL, m_wndStackTypeLabel);
	DDX_Control(pDX, IDC_HEIGHT, m_wndHeight);
	DDX_Control(pDX, IDC_GAP_DEPTH, m_wndGapDepth);
	DDX_Control(pDX, IDC_DEPTH, m_wndDepth);
	DDX_Control(pDX, IDC_TRANSPARENCY, m_wndTransparency);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_Slider(pDX, IDC_TRANSPARENCY, m_nTransparency);
	DDX_Check(pDX, IDC_DATA_LABELS, m_bShowDataLabels);
	DDX_CBIndex(pDX, IDC_STACK_TYPE, m_nStackType);
	DDX_Control(pDX, IDC_ROTATE, m_wndRotate);
	DDX_Slider(pDX, IDC_DEPTH, m_nDepth);
	DDX_Slider(pDX, IDC_GAP_DEPTH, m_nGapDepth);
	DDX_Slider(pDX, IDC_HEIGHT, m_nHeight);
	DDX_Check(pDX, IDC_SHOW_WALLS, m_bShowWalls);
	DDX_Check(pDX, IDC_THICK_WALLS, m_bThickWalls);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartArea3DView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartArea3DView)
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_DATA_LABELS, OnUpdateChart)
	ON_BN_CLICKED(IDC_ROTATE, OnRotate)
	ON_CBN_SELENDOK(IDC_STACK_TYPE, OnUpdateChart)
	ON_BN_CLICKED(IDC_SHOW_WALLS, OnUpdateChart)
	ON_BN_CLICKED(IDC_THICK_WALLS, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartArea3DView diagnostics

#ifdef _DEBUG
void CChartArea3DView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartArea3DView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartArea3DView message handlers

void CChartArea3DView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;

	if (m_Feature == CDemoFeature::BCGP_RangeArea3D)
	{
		m_wndStackType.EnableWindow(FALSE);
		m_wndStackType.ShowWindow(SW_HIDE);
		m_wndStackTypeLabel.ShowWindow(SW_HIDE);
	}

	m_wndTransparency.SetRange(0, 100, TRUE);

	m_wndDepth.SetRange(0, 1000, TRUE);
	m_wndDepth.SetPos(m_nDepth);

	m_wndGapDepth.SetRange(0, 90, TRUE);
	m_wndGapDepth.SetPos(m_nGapDepth);

	m_wndHeight.SetRange(0, 200, TRUE);
	m_wndHeight.SetPos(m_nHeight);

	m_wndRotate.GetRotationObject()->SetAutorepeatMode(100);
	m_wndRotate.GetRotationObject()->SetColorTheme(CBCGPRotationObject::BCGP_COLOR_THEME_VISUAL_MANAGER);

	m_wndRotate.GetRotationObject()->EnablePart(CBCGPRotationObject::BCGP_ROTATION_CLOCKWISE, FALSE);
	m_wndRotate.GetRotationObject()->EnablePart(CBCGPRotationObject::BCGP_ROTATION_COUNTER_CLOCKWISE, FALSE);

	m_wndRotate.GetRotationObject()->EnableFlatIcons();

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->SetChartType(BCGPChartArea3D, (m_Feature == CDemoFeature::BCGP_RangeArea3D) ? BCGP_CT_RANGE : BCGP_CT_SIMPLE);

	pChart->ShowAxisIntervalInterlacing(BCGP_CHART_Y_PRIMARY_AXIS, TRUE);
 	pChart->ShowAxisIntervalInterlacing(BCGP_CHART_X_PRIMARY_AXIS, FALSE);

	pChart->SetChartTitle(
		(m_Feature == CDemoFeature::BCGP_RangeArea3D) ?
		_T("Temperature Ranges by Month/Year:") :
		_T("Average Temperatures by Month/Year:"));

	CBCGPChartAreaSeries* pSeries1 = DYNAMIC_DOWNCAST(CBCGPChartAreaSeries, pChart->CreateSeries(m_arYears[2]));
	CBCGPChartAreaSeries* pSeries2 = DYNAMIC_DOWNCAST(CBCGPChartAreaSeries, pChart->CreateSeries(m_arYears[1]));
	CBCGPChartAreaSeries* pSeries3 = DYNAMIC_DOWNCAST(CBCGPChartAreaSeries, pChart->CreateSeries(m_arYears[0]));

	COleDateTime today = COleDateTime::GetCurrentTime();

	double arTemp[] = { 5., 12., 19, 22, 19, 24, 22, 24, 18, 14, 10, 7 };
	for (int nMonth = 1; nMonth <= 12; nMonth++)
	{
		COleDateTime date (today.GetYear(), nMonth, 1, 0, 0, 0);
		CString strMonth = date.Format(_T("%b"));

		if (m_Feature == CDemoFeature::BCGP_RangeArea3D)
		{
			pChart->AddChartDataYY1(strMonth, arTemp[nMonth - 1], Rand(2, 5), 0);
			pChart->AddChartDataYY1(strMonth, (int)(arTemp[nMonth - 1] + Rand(-5, 10)), Rand(2, 5), 1);
			pChart->AddChartDataYY1(strMonth, (int)(arTemp[nMonth - 1] + Rand(-5, 10)), Rand(2, 5), 2);
		}
		else
		{
			pSeries1->AddDataPoint(strMonth, arTemp[nMonth - 1]);
			pSeries2->AddDataPoint((int)(arTemp[nMonth - 1] + Rand(-5, 5)));
			pSeries3->AddDataPoint((int)(arTemp[nMonth - 1] + Rand(-5, 5)));
		}
	}

	OnUpdateChart();
}

void CChartArea3DView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	CBCGPChartExampleView::OnHScroll(nSBCode, nPos, pScrollBar);

	if (pScrollBar != NULL)
	{
		OnUpdateChart();
	}
}

void CChartArea3DView::OnUpdateChart()
{
	UpdateData();

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	CBCGPChartDiagram3D* pDiagram3D = pChart->GetDiagram3D();
	ASSERT(pDiagram3D != NULL);

	pChart->ShowDataLabels(m_bShowDataLabels, TRUE, FALSE, 0.);

	if (m_Feature != CDemoFeature::BCGP_RangeArea3D)
	{
		if (m_nStackType == 0)
		{
			pChart->SetChartType(BCGPChartArea3D, BCGP_CT_SIMPLE);
		}
		else if (m_nStackType == 1)
		{
			pChart->SetChartType(BCGPChartArea3D, BCGP_CT_STACKED);
		}
		else
		{
			pChart->SetChartType(BCGPChartArea3D, BCGP_CT_100STACKED);
		}
	}

	pChart->SetThemeOpacity(100 - m_nTransparency);

	pDiagram3D->SetDepthScalePercent(0.01 * m_nDepth);
	pDiagram3D->SetFrontDistancePercent(0.01 * m_nGapDepth);
	pDiagram3D->SetHeightScalePercent(0.01 * m_nHeight);

	DWORD dwoFlags = CBCGPChartDiagram3D::DWO_OUTLINE_ALL;

	// Draw walls and floor:
	if (m_bShowWalls)
	{
		dwoFlags |= (CBCGPChartDiagram3D::DWO_DRAW_ALL_WALLS | CBCGPChartDiagram3D::DWO_DRAW_FLOOR);
	}

	m_wndThickWalls.EnableWindow(m_bShowWalls);

	pDiagram3D->SetDrawWallOptions((CBCGPChartDiagram3D::DrawWallOptions)dwoFlags);
	pDiagram3D->SetThickWallsAndFloor(m_bShowWalls && m_bThickWalls);

	pChart->SetDirty();
	pChart->Redraw();
}

void CChartArea3DView::OnRotate() 
{
	RotateChart(m_wndRotate.GetRotationObject()->GetClicked());
}
